//init state
const state = () => ({
  param: [
    /*{
      adressMsgId: 1, //地址信息表编号
      contName: "邓志强", //用户名称
      contPhone: "13874298326", //用户电话
      areaAdress: ["湖南省","长沙市","雨花区"], //区域
      careAdress: "长沙一度", //详细地址
      selectStatus: 1, //选中状态 默认选中为1 非默认选中为2
      isNewAdd: false, //是否为新添加 统一赋值false
      ifLL: false //是否高亮显示，统一赋值false
    },
    {
      adressMsgId: 2,
      contName: "王小军",
      contPhone: "13874298326",
      areaAdress: ["湖南省","长沙市","雨花区"],
      careAdress: "长沙一度",
      selectStatus: 2,
      isNewAdd: false,
      ifLL: false
    },
    {
      adressMsgId: 3,
      contName: "黄雄株",
      contPhone: "13874298326",
      areaAdress: ["湖南省","长沙市","雨花区"],
      careAdress: "长沙一度",
      selectStatus: 2,
      isNewAdd: false,
      ifLL: false
    },
    {
      adressMsgId: 4,
      contName: "杨峥辉",
      contPhone: "13874298326",
      areaAdress: ["湖南省","长沙市","雨花区"],
      careAdress: "长沙一度",
      selectStatus: 2,
      isNewAdd: false,
      ifLL: false
    },
    {
      adressMsgId: 5,
      contName: "杨赛",
      contPhone: "13874298326",
      areaAdress: ["湖南省","长沙市","雨花区"],
      careAdress: "长沙一度",
      selectStatus: 2,
      isNewAdd: false,
      ifLL: false
    }*/
  ]
})

//getters
const getters = {
  getData(state){//获取所有数据
    return state.param;
  },
  getDataById(state){//通过编号查询数据
    return function (adressMsgId) {
      return state.param.filter(item => {
        return item.adressMsgId == adressMsgId;
      })[0];
    }
  },
  getIsLLAdressMsgId(state){//取出常亮的地址编号
    return state.param.filter(item => {
      return item.ifLL == true;
    })[0].adressMsgId;
  },
  refreshShippingAddressSessionStorageData(state){//刷新session中的方法
    sessionStorage.setItem("ShippingAddress",JSON.stringify(state.param))
  }
}

//actions
const actions = {
  addNewAds({commit},payload){
    commit("addNewAdsMut",payload)
  },
  updateAds({commit},payload){//修改地址值
    commit("updateAdsMut",payload)
  },
  deleteAds({commit},payload){//删除地址
    commit("deleteAdsMut",payload)
  },
  updateifLL({commit}){//修改所有选中状态
    commit("updateifLLMut")
  },
  loadDataForShippingAddress({commit},payload){//替换值
    commit("loadDataForShippingAddressMut",payload);
  }
}

//mutations
const mutations = {
  updateifLLMut(state){//修改所有选中状态
    state.param.filter(item => {
      item.ifLL = false;
    })
  },
  addNewAdsMut(state,val){
    state.param = [...state.param,val]
  },
  updateAdsMut(state,val){//修改地址值
    let tempVal = state.param.filter(item => {
      return item.adressMsgId == val.adressMsgId;
    })[0];
    tempVal.contName = val.contName;
    tempVal.contPhone = val.contPhone;
    tempVal.areaAdress = val.areaAdress;
    tempVal.careAdress = val.careAdress;
    tempVal.selectStatus = val.selectStatus;
    if (val.selectStatus == 1){//修改默认状态
      state.param.map(item => {
        if (item.adressMsgId != val.adressMsgId){
          item.selectStatus = 2;
        }
      })
    }
  },
  deleteAdsMut(state,val){//删除地址
    let index = 0;
    state.param.filter(item => {
      if (item.adressMsgId == val){
        //删除当前地址
        state.param.splice(index,1);
        //查看当前删除地址是否为默认地址,如果是默认地址则将第一个地址改为默认地址，并删除当前地址
        if(item.selectStatus == 1 && state.param.length != 0) state.param[0].selectStatus = 1;
      }
      ++index;
    });
  },
  loadDataForShippingAddressMut(state,val){
    state.param = [...val];
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
